home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / Fl_add_idle.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-07  |  2.3 KB  |  90 lines

  1. //
  2. // "$Id: Fl_add_idle.cxx,v 1.4 1999/01/07 19:17:30 mike Exp $"
  3. //
  4. // Idle routine support for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. // Allows you to manage an arbitrary set of idle() callbacks.
  27. // Replaces the older set_idle() call (which is used to implement this)
  28.  
  29. #include <FL/Fl.H>
  30.  
  31. struct idle_cb {
  32.   void (*cb)(void*);
  33.   void* data;
  34.   idle_cb *next;
  35. };
  36.  
  37. // the callbacks are stored linked in a ring.  last points at the one
  38. // just called, first at the next to call.  last->next == first.
  39.  
  40. static idle_cb* first;
  41. static idle_cb* last;
  42. static idle_cb* freelist;
  43.  
  44. static void call_idle() {
  45.   idle_cb* p = first;
  46.   last = p; first = p->next;
  47.   p->cb(p->data); // this may call add_idle() or remove_idle()!
  48. }
  49.  
  50. void Fl::add_idle(void (*cb)(void*), void* data) {
  51.   idle_cb* p = freelist;
  52.   if (p) freelist = p->next;
  53.   else p = new idle_cb;
  54.   p->cb = cb;
  55.   p->data = data;
  56.   if (first) {
  57.     last->next = p;
  58.     p->next = first;
  59.     first = p;
  60.   } else {
  61.     first = last = p;
  62.     p->next = p;
  63.     set_idle(call_idle);
  64.   }
  65. }
  66.  
  67. void Fl::remove_idle(void (*cb)(void*), void* data) {
  68.   idle_cb* p = first;
  69.   if (!p) return;
  70.   idle_cb* l = last;
  71.   for (;; p = p->next) {
  72.     if (p->cb == cb && p->data == data) break;
  73.     if (p==last) return; // not found
  74.     l = p;
  75.   }
  76.   if (l == p) { // only one
  77.     first = last = 0;
  78.     set_idle(0);
  79.   } else {
  80.     last = l;
  81.     first = l->next = p->next;
  82.   }
  83.   p->next = freelist;
  84.   freelist = p;
  85. }
  86.  
  87. //
  88. // End of "$Id: Fl_add_idle.cxx,v 1.4 1999/01/07 19:17:30 mike Exp $".
  89. //
  90.